package com.situ.ftbserver.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.ftbserver.mapper.MemberMapper;
import com.situ.ftbserver.model.Member;
import com.situ.ftbserver.model.search.MemberSearchBean;
import com.situ.ftbserver.service.MemberService;
import com.situ.ftbserver.service.ReserveService;
import com.situ.ftbserver.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
@CacheConfig(cacheNames = "memberServiceImpl")
@Service
public class MemberServiceImpl implements MemberService {


    private MemberMapper memberMapper;
    private ReserveService reserveService;

    @Autowired
    public void setMemberMapper(MemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }
    @Autowired
    public void setReserveService(ReserveService reserveService) {
        this.reserveService = reserveService;
    }

    @Override
    public Page<Member> findAll( Page<Member> page, MemberSearchBean msb) {
        return memberMapper.findAll(page, msb);
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Integer[] ids) {
        if(reserveService.findByMemberId(ids).size()>0){
            return 0;
        }
        return memberMapper.deleteByIds(Arrays.asList(ids));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Member member) {

        return memberMapper.insert(member) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean updateById(Member member) {
        return memberMapper.updateById(member) > 0;
    }

    @Cacheable(key = "'getMember'")
    @Override
    public List<Member> getMember() {
        return memberMapper.selectList( null);
    }

    @Cacheable(key = "'getMemberAgeCount'")
    @Override
    public List<Map<String, Integer>> getMemberAgeCount() {
        return memberMapper.getMemberAgeCount();
    }
}
